<?php
class option
{
    private $db;
    private $table;

    public $pages;//返回分页URL

    public function __construct() {
        global $db;
        $this->db = &$db;
        $this->table = DB_PRE.'investigate_option';
    }

    /**
	 * 添加调查问卷选项信息
	 *
	 * @param ARRAY() $data
	 * @return 	INT or FALSE
	 */
    public function add( $data )
    {
        $params = $data;
        $res = $this->db->insert( $this->table, $params );
		return $res;
    }

    /**
	 * 获取单条调查问卷选项信息
	 *
	 * @param INT $id
	 * @return ARRAY $option
	 */
    public function get( $id )
    {
        $this->db->prepare( "SELECT * FROM $this->table WHERE status=? and id=? LIMIT 1" );
        $this->db->set_int( STATUS_NORMAL );
        $this->db->set_string( $id );
		$option = $this->db->execute( true );
		return $option;
    }

    /**
	 * 修改调查问卷选项信息
	 *
	 * @param ARRAY $data
	 * @param INT $id
	 * @return BOOL
	 */
    public function edit( $data, $id )
    {
        $params = $data;
		$res = $this->db->simple_update( $this->table, $params, array( 'id'=>$id ) );
		return $res;
    }


    /**
	 * 删除调查问卷选项信息
	 *
	 * @param INT $id
	 * @return BOOL
	 */
    public function del( $id )
    {
        return $this->edit( array( 'status'=>STATUS_DEL ), $id );
    }

    /**
	 * 查询所有调查问卷选项列表信息
	 *
	 * @param ARRAY() $params, STRING $order, INT $page, INT $pagesize
	 * @return 	ARRAY() $list
	 */
    public function get_list( $params, $order, $page=1, $pagesize=10 )
    {
        $where = '';
        if( is_array( $params ) && sizeof( $params ) > 0 )
        {
            $where = ' where ';
            $where .= implode( ' and ', $params );
        }
        $where .= $order;
        $this->db->prepare( "SELECT count(*) as nums FROM $this->table" . $where );
		$res = $this->db->execute( true );
		$nums = isset( $res['nums'] ) ? intval( $res['nums'] ) : 0;
        $this->pages = pages( $nums, $page, $pagesize );
		$this->db->prepare( "SELECT * FROM $this->table" . $where . " limit ?, ?" );
		$this->db->set_int( ( $page-1 ) * $pagesize );
		$this->db->set_int( $pagesize );
	    $list = $this->db->execute();
        return $list;
    }

    /**
	 * 调查问卷选项信息排序
	 *
	 * @param ARRAY() $data
	 * @return 	BOOL
	 */
    public function listorder( $data )
    {
        if( !is_array( $data ) )
        {
            return false;
        }
        foreach( $data as $id => $listorder )
        {
            $listorder = intval( $listorder );
            $this->edit( array( 'listorder' => $listorder ), $id );
        }
        return true;
    }

}
?>
